package com.yhq.basic.thread.lock;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author hqyin
 * @date 2025/5/20 12:36 PM
 */
public class Demo2ReentrantLock {
    private static final Lock lock = new ReentrantLock();
    private static final Condition condition = lock.newCondition();
    public static void main(String[] args) throws InterruptedException {
        //分析AbstractQueuedSynchronizer (AQS) 实现原理
//        test_AQS();
        //自定义独占锁
        test_exclusiveOwner();

    }

    private static int sum=0;
    public static void test_exclusiveOwner() throws InterruptedException {
        Lock lock = new ReentrantLock();
//        ExclusiveOwnerSelfLock lock = new ExclusiveOwnerSelfLock();
        for (int i = 0; i < 3; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    lock.lock();
                    System.out.println(Thread.currentThread().getName()+"获取锁成功");
                    try {
                        Thread.sleep(200000);
                        for (int j = 0; j < 10000; j++) {
                            sum++;
                        }
                    }catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        System.out.println(Thread.currentThread().getName()+"释放锁");
                        lock.unlock();
                    }

                }
            },"t"+i).start();

        }
        Thread.sleep(2000);
        System.out.println(sum);
    }

    private static void test_AQS() throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();

                System.out.println(Thread.currentThread().getName()+"加锁");
                try {
                    condition.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    System.out.println(Thread.currentThread().getName()+"释放锁");
                    lock.unlock();
                }

            }
        },"线程1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"加锁");
                try {
                    condition.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    System.out.println(Thread.currentThread().getName()+"释放锁");
                    lock.unlock();
                }

            }
        },"线程2").start();

        Thread.sleep(10000);
        lock.lock();
        System.out.println(Thread.currentThread().getName()+"加锁");
        condition.signalAll();
        System.out.println(Thread.currentThread().getName()+"释放锁");
        lock.unlock();
    }
}
